package com.zzedu.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.zzedu.common.constant.UserConstants;
import com.zzedu.system.domain.SysPost;
import com.zzedu.system.mapper.SysPostMapper;
import com.zzedu.system.mapper.SysUserPostMapper;
import com.zzedu.system.service.ISysPostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * 岗位信息 服务层处理
 *
 * @author smarthome
 */
@Service
public class SysPostServiceImpl extends ServiceImpl<SysPostMapper, SysPost> implements ISysPostService {
    @Autowired
    private SysUserPostMapper userPostMapper;

    /**
     * 查询岗位信息集合
     *
     * @param post 岗位信息
     * @return 岗位信息集合
     */
    @Override
    public IPage<SysPost> selectPostList(IPage<SysPost> page, SysPost post) {
        LambdaQueryWrapper<SysPost> queryWrapper = Wrappers.lambdaQuery(SysPost.class);
        queryWrapper.eq(null != post.getStatus(), SysPost::getStatus, post.getStatus());
        queryWrapper.eq(StringUtils.isNotBlank(post.getPostCode()), SysPost::getPostCode, post.getPostCode());
        queryWrapper.like(StringUtils.isNotBlank(post.getPostName()), SysPost::getPostName, post.getPostName());
        queryWrapper.orderByDesc(SysPost::getId);
        return page(post.getPageDTO(), queryWrapper);
    }

    /**
     * 查询所有岗位
     *
     * @return 岗位列表
     */
    @Override
    public List<SysPost> selectPostAll() {
        return this.list();
    }


    /**
     * 根据用户ID获取岗位选择框列表
     *
     * @param userId 用户ID
     * @return 选中岗位ID列表
     */
    @Override
    public List<Long> selectPostListByUserId(Long userId) {
        LambdaQueryWrapper<SysPost> queryWrapper = Wrappers.lambdaQuery(SysPost.class);
        queryWrapper.apply("id in(select post_id from sys_user_post where deleted=0 and user_id={0})", userId);
        return Optional.ofNullable(this.list(queryWrapper)).orElse(Lists.newArrayList()).stream().map(SysPost::getId).collect(Collectors.toList());
    }
}
